﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Services;
using System.Web.Script.Services;
using  LotteCinema.BUS;
using LotteCinema.DTO;

namespace LotteCinema.Admin
{
    public partial class NhanVien : System.Web.UI.Page
    {
        static BUS.BNhanVien bnv = new BUS.BNhanVien();
        static DAO.CNhanVien cnv = new DAO.CNhanVien();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    LayTatCa();
                    pnNhanVien.Visible = true;
                    pnThem.Visible = false;
                    pnCapNhat.Visible = false;
                }
                catch (Exception ex)
                {
                    DTO.WebMegBox.Show(ex.Message.ToString());
                }
            }
        }
        #region Khởi tạo dữ liệu control
        public void KhoiTaoDuLieu()
        {
            txtmanhanvien.Text = "";
            txtmatkhau.Text = "";
            txthoten.Text = "";
            txtcmnd.Text = "";
            txtngaysinh.Text = "";
            txtdiachi.Text = "";
            txtdienthoai.Text = "";
        }
        #endregion
        
        #region Lấy tất cả thông tin nhân viên
        public void LayTatCa()
        {
            try
            {
                grdNhanVien.DataSource = bnv.NHANVIEN_LayTatCa();
                grdNhanVien.DataBind();
            }
            catch (Exception ex)
            {
                DTO.WebMegBox.Show(ex.Message.ToString());
            }
        }
        #endregion

        protected void btnThemMoi_Click(object sender, EventArgs e)
        {
            KhoiTaoDuLieu();
            try
            {
                DataTable dt = bnv.NHANVIEN_LayTatCaChucVu();
                ddlchucvu.DataSource = dt;
                ddlchucvu.DataValueField = "MACV";
                ddlchucvu.DataTextField = "TENCV";
                ddlchucvu.DataBind();
                ddlgioitinh.Items.Add("Nam");
                ddlgioitinh.Items.Add("Nữ");
                pnNhanVien.Visible = false;
                pnCapNhat.Visible = false;
                pnThem.Visible = true;
                lbtThem.Visible = true;
                Session["chucnang"] = "them";
            }
            catch (Exception ex)
            {
                DTO.WebMegBox.Show(ex.Message.ToString());
            }
        }
        protected void grdNhanVien_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            cnv._manv = e.CommandArgument.ToString();
            if (e.CommandName == "Edit")
            {
                try
                {
                    DataTable dt = bnv.NHANVIEN_LayTatCaChucVu();
                    ddlCapNhatChucVu.DataSource = dt;
                    ddlCapNhatChucVu.DataValueField = "MACV";
                    ddlCapNhatChucVu.DataTextField = "TENCV";
                    ddlCapNhatChucVu.DataBind();
                    pnNhanVien.Visible = false;
                    pnThem.Visible = false;
                    lbtThem.Visible = false;
                    pnCapNhat.Visible = true;
                    Session["chucnang"] = "capnhat";
                }
                catch (Exception ex)
                { DTO.WebMegBox.Show(ex.Message.ToString()); }
               
            } 
            else
                if (e.CommandName == "Delete")
                {
                    try
                    {
                        bnv.NHANVIEN_Xoa(cnv);
                        LayTatCa();
                    }
                    catch (Exception ex)
                    {
                        DTO.WebMegBox.Show(ex.Message.ToString());
                    }
                }
        }
        protected void grdNhanVien_PhanTrang(object source, DataGridPageChangedEventArgs e) 
        {
            try
            {
                grdNhanVien.CurrentPageIndex = +e.NewPageIndex;
                LayTatCa();
            }
            catch (Exception ex)
            { DTO.WebMegBox.Show(ex.Message.ToString()); }
        }

        protected void lbtThem_Click(object sender, EventArgs e)
        {
            try
            {
                
                cnv._manv = txtmanhanvien.Text;
                cnv._matkhau = txtmatkhau.Text;
                cnv._hoten = txthoten.Text;
                cnv._cmnd = txtcmnd.Text;
                cnv._ngaysinh = txtngaysinh.Text;
                cnv._gioitinh = ddlgioitinh.SelectedValue == "Nam" ? "True" : "False";
                cnv._diachi = txtdiachi.Text;
                cnv._dienthoai = txtdienthoai.Text;
                cnv._macv = ddlchucvu.SelectedValue;
                bnv.NHANVIEN_Them(cnv);
                LayTatCa();
            }
            catch (Exception ex)
            { DTO.WebMegBox.Show(ex.Message.ToString()); }
        }
        protected void lblQuayLai_Click(object sender, EventArgs e)
        {
            try
            {
               
                LayTatCa();
                pnNhanVien.Visible = true;
                pnThem.Visible = false;
                pnCapNhat.Visible = false;
                Session["chucnang"] = null;
            }
            catch (Exception ex)
            { DTO.WebMegBox.Show(ex.Message.ToString()); }
        }

        protected void lblCapNhatChucVu_Click(object sender, EventArgs e)
        {
            try
            {
                cnv._macv = ddlCapNhatChucVu.SelectedValue;
                bnv.NHANVIEN_CapNhatChucVu(cnv);
            }
            catch (Exception ex)
            { DTO.WebMegBox.Show(ex.Message.ToString()); }
        }

        protected void lblQuayLai2_Click(object sender, EventArgs e)
        {
            try
            {
                
                LayTatCa();
                pnNhanVien.Visible = true;
                pnThem.Visible = false;
                pnCapNhat.Visible = false;
            }
            catch (Exception ex)
            { DTO.WebMegBox.Show(ex.Message.ToString()); }
        }

        [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
        public static string[] GetKeyWords(string prefixText)
        {
            DataTable dt = new DataTable();
            try
            {
                dt = bnv.NHANVIEN_TimKiem(prefixText);
            }
            catch (Exception ex)
            {
                DTO.WebMegBox.Show(ex.Message.ToString()); 
            }
                string[] items = new string[dt.Rows.Count];
                int i = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    items.SetValue(dr["HOTEN"].ToString(), i);
                    i++;
                }

            return items;
        }

        protected void bttimkiem_Click(object sender, EventArgs e)
        {
            try
            {
                grdNhanVien.DataSource = bnv.NHANVIEN_TimKiem(txtSearch.Text);
                grdNhanVien.DataBind();
            }
            catch (Exception ex)
            { DTO.WebMegBox.Show(ex.Message.ToString()); }
        }
    }
}